<?php

class Oper extends Controller {

	function index()
	{
		global $TYPE_OPER;

		$me = $this->MUser->get(Auth::$user_id);
		$me['children'] = $this->MUser->getList(array('parent_id' => Auth::$user_id, 'sort' => 'level desc,fio asc'));
		$structure = array($me['id'] => $me);

		$is_open_filter = !(@$_REQUEST['t'] || @$_REQUEST['u'] || @$_REQUEST['col'] || @$_REQUEST['last_oper']);

		$filter_opers = array();

		$type_oper = array(
			'reg' => $TYPE_OPER['reg'],
			'buy' => $TYPE_OPER['buy'],
			'ng' => $TYPE_OPER['ng'],
			'ref' => $TYPE_OPER['ref'],
			'week' => $TYPE_OPER['week'],
			'modify' => 'Переоформление',
			'sell' => $TYPE_OPER['sell'],
		);
		foreach($TYPE_OPER as $k => $v)
		{
			if(substr($k, 0, 2) != 'l*')
			{
				continue;
			}
			$l = str_replace("l", "", $k);
			if(strlen($l) <= strlen($me['level']))
			{
				$type_oper[$k] = $v;
			}
			elseif($this->MOper->getCount(array('user_id' => $me['id'], 'type' => $k)))
			{
				$type_oper[$k] = $v;
			}
			else
			{
				break;
			}
		}

		$filter = array();

		$filter['type'] = isset($_REQUEST['t']) ? (is_array(@$_REQUEST['t']) ? $_REQUEST['t'] : explode("-", $_REQUEST['t'])) : array();
		$types = array();
		foreach($filter['type'] as $v)
		{
			if($v == 'modify')
			{
				$types[] = 'modify.sell';
				$types[] = 'modify.buy';
			}
			elseif($v == 'sell')
			{
				$types[] = 'sell.mass';
				$types[] = 'sell';
			}
			elseif($v == 'ng')
			{
				$types[] = 'ng';
				$types[] = 'patriot';
				$types[] = 'arest';
				$types[] = '8marta';
				$types[] = 'sell.fire.ng';
				$types[] = 'sell.fire.patriot';
				$types[] = 'sell.fire.arest';
				$types[] = 'sell.fire.8marta';
			}
			elseif($v == 'ref')
			{
				$types[] = 'ref';
				$types[] = 'sell.fire.ref';
			}
			elseif(substr($v, 0, 2) == 'l*')
			{
				if(isset($TYPE_OPER[$v]))
				{
					$types[] = $v;
					$types[] = 'sell.fire.'.$v;
				}
			}
			else
			{
				if(isset($TYPE_OPER[$v]))
				{
					$types[] = $v;
				}
			}
		}
		if(count($types))
		{
			$filter_opers['type'] = $types;
		}

		$cols = array('frozen');
		$filter['col'] = @$_GET['f'] ? (is_array(@$_REQUEST['col']) ? $_REQUEST['col'] : array()) : $cols;

		$filter_users = array();
		$filter_users['hi_like'] = $me['hi'];
		$filter_users['sort'] = 'level desc,fio asc';
		$filter['users_id'] = isset($_REQUEST['u']) ? (is_array(@$_REQUEST['u']) ? $_REQUEST['u'] : explode("-", $_REQUEST['u'])) : array();
		foreach($filter['users_id'] as $k => $u_id)
		{
			$filter['users_id'][$k] = intval($u_id);
		}
		$filter['inc_des'] = @$_REQUEST['inc_des'] ? (is_array(@$_REQUEST['inc_des']) ? $_REQUEST['inc_des'] : array($_REQUEST['inc_des'])) : array();
		if(@$_REQUEST['inc_des'])
		{
			$inc_des = is_array($_REQUEST['inc_des']) ? $_REQUEST['inc_des'] : array($_REQUEST['inc_des']);
			foreach($inc_des as $inc_des_id)
			{
				if(!in_array($inc_des_id, $filter['users_id']))
				{
					continue;
				}
				$children = $this->MUser->getList(array('parent_id' => $inc_des_id));
				$filter['users_id'] = array_merge($filter['users_id'], array_keys($children));
			}
		}
		else
		{
			$filter_users['parent_id'] = $me['id'];
		}
		$filter_users['id'] = count($filter['users_id']) ? $filter['users_id'] : -1;
		$users = $this->MUser->getList($filter_users);
		$users2 = array();
		if(in_array($me['id'], $filter['users_id']))
		{
			$users2[$me['id']] = $me;
			$users[$me['id']] = $me;
		}
		$allreferer_emails = array();
		foreach($users as $k => $user)
		{
			$user['operations'] = array();
			$users[$k] = $user;
			$users_email_index[$user['email']] = $user;
			$allreferer_emails[$user['referer_email']] = $user['referer_email'];
			$users2[$user['id']] = $user;
		}
		$filter_opers['sort'] = 'date asc,id asc';
		$filter_opers['user_id'] = count($users2) ? array_keys($users2) : -1;
		$all_platezh_id = array();
		$all_opers = $this->MOper->getList($filter_opers);

		if(isset($_REQUEST['last_oper']) && $_REQUEST['last_oper'])
		{
			$last_oper_id = intval($_REQUEST['last_oper']);
			$filter['last_oper'] = $last_oper_id;
			$filter_users_last_oper = $filter_users;
			$filter_users_last_oper['id'] = array();
			unset($filter_users_last_oper['parent_id']);
			$all_opers = array();
			$all_opers_tmp = array();
			$last_oper = $this->MOper->get($last_oper_id);
			$all_opers_tmp[] = $last_oper;
			$last_oper_parent_id = $last_oper['parent_id'];
			$i = 0;
			while($last_oper_parent_id)
			{
				if($i >= 30) break;
				$i++;
				$last_oper_parent = $this->MOper->get($last_oper_parent_id);
				if(@$last_oper_parent['id'])
				{
					$all_opers_tmp[] = $last_oper_parent;
				}
				$last_oper_parent_id = @$last_oper_parent['parent_id'];
			}
			$all_opers_tmp = array_merge($all_opers_tmp, $this->MOper->getList(array('parent_id' => $last_oper_id)));
			foreach($all_opers_tmp as $oper)
			{
				$all_opers[$oper['id']] = $oper;
				$filter_users_last_oper['id'][] = $oper['user_id'];
			}
			$filter_users_last_oper['id'] = count($filter_users_last_oper['id']) ? $filter_users_last_oper['id'] : -1;
			$users = $this->MUser->getList($filter_users_last_oper);
			$users2 = array();
			$users_email_index = array();
			$allreferer_emails = array();
			foreach($users as $k => $user)
			{
				$user['operations'] = array();
				$users[$k] = $user;
				$users_email_index[$user['email']] = $user;
				$allreferer_emails[$user['referer_email']] = $user['referer_email'];
				$users2[$user['id']] = $user;
			}
			ksort($all_opers);
		}

		foreach($all_opers as $oper)
		{
			if(isset($users2[$oper['user_id']]))
			{
				@$users2[$oper['user_id']]['operations'][$oper['id']] = $oper;
			}
			if(in_array('platezh', $filter['col']) && $oper['platezh_id'])
			{
				$all_platezh_id[] = $oper['platezh_id'];
			}
		}
		$all_mavro_summ = $this->MOper->getMavroSumm(array_keys($users2));
		foreach($all_mavro_summ as $u_id => $valutas)
		{
			$users2[$u_id]['all_mavro'] = $valutas;
		}

		if(in_array('platezh', $filter['col']))
		{
			$all_parents_id = array();
			if(!count($all_platezh_id))
			{
				$all_platezh_id = -1;
			}
			$all_platezhi = $this->MPlatezh->getList(array('t.id' => $all_platezh_id, 'add_fields' => array('schet_name')));
			$relation_opers = array();
			foreach($users2 as $k => $u)
			{
				foreach($u['operations'] as $kk => $o)
				{
					$users2[$k]['operations'][$kk]['_platezh'] = @$all_platezhi[$o['platezh_id']];
					$users2[$k]['operations'][$kk]['_add_platezh'] = in_array($o['type'], array('buy', 'sell', 'sell.mass')) && !@$users2[$k]['operations'][$kk]['_platezh']['id'];
					if(($o['type'] == 'ref' || substr($o['type'], 0, 2) == 'l*') && $o['parent_id'])
					{
						$all_parents_id[$k][] = $o['id'];
						$all_parents_id2[] = $o['parent_id'];
					}
					$relation_opers[$o['id']] = $o['parent_id'];
				}
			}
			if(count($all_parents_id))
			{
				$parent_platezhi = $this->MPlatezh->getPlatezhiByOpers($all_parents_id2);
				foreach($all_parents_id as $u_id => $arr_parents)
				{
					foreach($arr_parents as $o_id)
					{
						if(@$users2[$u_id] && @$relation_opers[$o_id] && @$parent_platezhi[$relation_opers[$o_id]])
						{
							$users2[$u_id]['operations'][$o_id]['_platezh'] = $parent_platezhi[$relation_opers[$o_id]];
						}
					}
				}
			}
		}

		$referer_users = array();
		$all_referer_operations_id = array();
		foreach($allreferer_emails as $r_email)
		{
			if(isset($users_email_index[$r_email]))
			{
				continue;
			}
			$user_referer = $this->MUser->getFirstRow(array('email' => $r_email));
			if(@$user_referer['id'])
			{
				$filters = array('user_id' => $user_referer['id'], 'type' => 'ref', 'parent_id' => array_keys($all_opers), 'sort' => 'date asc,id asc');
				$user_referer['operations'] = count($all_opers) ? $this->MOper->getList($filters) : array();
				$all_referer_operations_id = array_merge($all_referer_operations_id, array_keys($user_referer['operations']));
				$referer_users[$user_referer['id']] = $user_referer;
			}
		}
		$all_mavro_summ_referer = $this->MOper->getMavroSumm(array_keys($referer_users), $all_referer_operations_id);
		foreach($all_mavro_summ_referer as $u_id => $valutas)
		{
			$referer_users[$u_id]['all_mavro'] = $valutas;
		}


		$this->load->view('head', array());
		$this->load->view('oper/index', array('users' => $users2, 'structure' => $structure, 'filter' => $filter, 'referer_users' => $referer_users, 'type_oper' => $type_oper, 'is_open_filter' => $is_open_filter));
		$this->load->view('foot');
	}

	function edit($id = 0)
	{
		$id = intval($id);
		if($id == 0)
		{
			return;
		}
		$oper = $this->MOper->get($id);
		if(!$this->MUser->isCanOper($oper['user_id']))
		{
			return;
		}
		if(!canEdit($oper))
		{
			return;
		}
		if(@$_POST['ok'])
		{
			$save = array(
				'hranen' => $_POST['hranen'],
				'dop' => $_POST['dop'],
			);
			$this->MOper->save($save, $id);
			redirect('oper/?u[]='.$oper['user_id']);
		}
		else
		{
			$user = $this->MUser->get($oper['user_id']);
			$this->load->view('head', array());
			$this->load->view('oper/edit', array('oper' => $oper, 'user' => $user));
			$this->load->view('foot');
		}
	}

	function delete($id)
	{
		$id = intval($id);
		if($id == 0)
		{
			return;
		}
		$oper = $this->MOper->get($id);
		if(!$this->MUser->isCanOper($oper['user_id']))
		{
			return;
		}
		if(!canDelete($oper))
		{
			return;
		}
		$this->load->library('Operation');
		if(@$_POST['ok'])
		{
			$platezhi_id = array();
			if($oper['platezh_id'])
			{
				$platezhi_id[] = $oper['platezh_id'];
			}
			$opers_id = @$_POST['other_oper_id'] && is_array($_POST['other_oper_id']) ? $_POST['other_oper_id'] : array('id' => -1);
			$opers = $this->MOper->getList(array('id' => $opers_id));
			foreach($opers as $o)
			{
				if($o['platezh_id'])
				{
					$platezhi_id[] = $o['platezh_id'];
				}
			}
			if(count($platezhi_id))
			{
				$scheta = $this->MSchet->getList(array('user_id' => Auth::$user_id));
				$platezhi = $this->MPlatezh->getList(array('id' => $platezhi_id, 'schet_id' => array_keys($scheta), 'add_fields' => array('can_delete')));
				foreach($platezhi as $k => $p)
				{
					$koeff = isPlatezhBuy($p) ? -1 : 1;
					$p['itogo'] = $p['summ'] + $koeff * $p['komiss'];
					platezhFromTo($p);
					$platezhi[$k] = $p;
				}
				$other_oper_id = @$_POST['other_oper_id'] && is_array($_POST['other_oper_id']) ? $_POST['other_oper_id'] : array();
				$this->load->view('head', array());
				$this->load->view('oper/delete_platezh', array('platezhi' => $platezhi, 'scheta' => $scheta, 'other_oper_id' => $other_oper_id));
				$this->load->view('foot');
			}
			else
			{
				$url = 'oper/delete/'.$oper['id'].'/?ok2=1';
				if(@$_POST['other_oper_id'] && is_array($_POST['other_oper_id']))
				{
					foreach($_POST['other_oper_id'] as $o_id)
					{
						$url .= "&other_oper_id[]=".$o_id;
					}
				}
				redirect($url);
			}
			return;
		}
		if(@$_REQUEST['ok2'])
		{
			$this->MOper->delete($id);
			if(@$_REQUEST['other_oper_id'] && is_array($_REQUEST['other_oper_id']) && count($_REQUEST['other_oper_id']))
			{
				foreach($_REQUEST['other_oper_id'] as $o_id)
				{
					$this->MOper->delete($o_id);
				}
			}
			if(@$_REQUEST['platezh_id'] && is_array($_REQUEST['platezh_id']) && count($_REQUEST['platezh_id']))
			{
				$scheta = $this->MSchet->getList(array('user_id' => Auth::$user_id));
				$platezhi = $this->MPlatezh->getList(array('id' => $_REQUEST['platezh_id'], 'schet_id' => array_keys($scheta), 'add_fields' => array('can_delete')));
				foreach($platezhi as $p)
				{
					if($p['can_delete'])
					{
						for($i = 1; $i <= 5; $i++)
						{
							if(@$p['file'.$i])
							{
								@unlink(UPLOAD_PATH.$p['file'.$i]);
							}
						}
						$this->MPlatezh->delete($p['id']);
					}
				}
			}
			redirect('oper/?u[]='.$oper['user_id']);
			die();
		}
		$user = $this->MUser->get($oper['user_id']);
		$user['operations'] = array($oper['id'] => $oper);
		$other_operations = array();
		if($oper['type'] == 'l*')
		{
			$other_operations = $this->_getOtherDes($oper['user_id'], $oper['date'], $oper['parent_id'], '*');
			foreach($other_operations as $k => $o)
			{
				$o['user'] = $this->MUser->get($o['user_id']);
				$other_operations[$k] = $o;
				$fire = $this->MOper->getFirstRow(array('type' => 'sell.fire.'.$o['type'], 'parent_id' => $o['id']));
				if(@$fire['id'])
				{
					$fire['user'] = $o['user'];
					$other_operations[$fire['id']] = $fire;
				}
			}
		}
		if($oper['type'] == 'modify.buy')
		{
			$other_operations = $this->MOper->getList(array('type' => 'modify.sell', 'id' => $oper['parent_id'], 'user_id' => $oper['user_id']));
			foreach($other_operations as $k => $o)
			{
				$o['user'] = $this->MUser->get($o['user_id']);
				$other_operations[$k] = $o;
			}
		}
		if($oper['type'] == 'sell')
		{
			global $USER_LEVEL;
			$root = $this->operation->getRoot($oper);
			$this->_addFire($root, 'ng', $user, $other_operations);
			$this->_addFire($root, 'patriot', $user, $other_operations);
			$this->_addFire($root, 'arest', $user, $other_operations);
			$this->_addFire($root, '8marta', $user, $other_operations);
			$this->_addFire($root, 'ref', $user, $other_operations);
			foreach($USER_LEVEL as $k => $v)
			{
				$this->_addFire($root, 'l'.$k, $user, $other_operations);
			}
		}
		$children = $this->operation->getAllChildren($oper);
		foreach($children as $o)
		{
			if(isset($other_operations[$o['id']]))
			{
				continue;
			}
			$o['user'] = $this->MUser->get($o['user_id']);
			if(@$o['user']['id'])
			{
				$other_operations[$o['id']] = $o;
			}
		}

		$this->load->view('head', array());
		$this->load->view('oper/delete', array('oper' => $oper, 'user' => $user, 'other_operations' => $other_operations));
		$this->load->view('foot');
	}

	function _addFire($root, $type, $user, &$operations)
	{
		$out = array();
		$bonus = $this->MOper->getFirstRow(array('type' => $type, 'parent_id' => $root['id']));
		if(@$bonus['id'])
		{
			$fire = $this->MOper->getFirstRow(array('type' => 'sell.fire.'.$type, 'parent_id' => $bonus['id']));
			if(@$fire['id'])
			{
				$fire['user'] = $fire['user_id'] == $user['id'] ? $user : $this->MUser->get($fire['user_id']);
				$operations[$fire['id']] = $fire;
			}
		}
	}

	function _getOtherDes($user_id, $date, $parent_id, $level = '')
	{
		$des_id = $this->MUser->getDesyatnikId($user_id, $date);
		$out = array();
		while($des_id)
		{
			$level .= "*";
			$operations_des = $this->MOper->getList(array('type' => 'l'.$level, 'date' => $date, 'user_id' => $des_id, 'parent_id' => $parent_id));
			$out = array_merge($out, $operations_des);
			$new_des_id = $this->MUser->getDesyatnikId($des_id, $date);
			if($new_des_id == $des_id)
			{
				$this->session->set_flashdata('system_error', 'Произошла внутреняя ошибка программы. Обратитесь к разработчикам');
				redirect('.');
			}
			$des_id = $new_des_id;
		}
		return $out;
	}

	function add_bonus_reg()
	{
		$users = $this->MUser->getChildrenAndMe(Auth::$user_id);
		$users_percent = array();
		$users_date = array();
		$kurses = $this->MKurs->getList();
		$users_l = array();
		foreach($users as $k => $user)
		{
			if($this->MOper->getCount(array('user_id' => $user['id'], 'type' => 'reg')) > 0)
			{
				unset($users[$k]);
				continue;
			}
			$users_l[$user['id']] = $this->MUser->isLgota($user, $user['date']) ? 1 : 0;
			$users_date[$user['id']] = $user['date'];
		}
		$kurses_date = array();
		foreach($kurses as $kurs)
		{
			$kurses_date[$kurs['date']][intval($kurs['percent'])] = array('buy' => floatval($kurs['buy']));
		}
		$this->load->view('head', array());
		$this->load->view('oper/add_bonus_reg', array('users' => $users, 'users_l' => $users_l, 'users_date' => $users_date, 'kurses' => $kurses_date));
		$this->load->view('foot');
	}

	function save_bonus_reg()
	{
		global $VALUTA;
		$user_id = intval(@$_POST['user_id']);
		if(!$this->MUser->isCanOper($user_id))
		{
			return;
		}
		$user = $this->MUser->get($user_id);
		$percent = getPercentByType($_POST['mavro_type_buy'], @$_POST['sverh'], $user, $user['date']);
		$user_is_lgota = $this->MUser->isLgota($user, $user['date'], 'reg');
		$depo = getDepoMonth($user_is_lgota, $percent);
		$save = array(
			'user_id' => $user['id'],
			'date' => $user['date'],
			'type' => 'reg',
			'bonus' => 1,
			'summ' => 20,
			'valuta' => '$',
			'mavro_valuta' => trim($_POST['mavro_valuta']),
			'kurs' => $this->MKurs->getKurs($user['date'], $percent, 'buy'),
			'percent' => $percent,
			'depo' => $depo,
			'hranen' => 'бонус за регистрацию',
		);
		$save['kurs_valuta'] = $save['mavro_valuta'] == $save['valuta'] ? 1 : getFloat(@$_POST['kurs_valuta']);
		$save['kurs_valuta'] = $save['kurs_valuta'] > 0 ? $save['kurs_valuta'] : 1;
		$save['mavro'] = @$_POST['kurs_valuta_reverse'] ? $save['summ'] / $save['kurs_valuta'] / $save['kurs'] : $save['summ'] * $save['kurs_valuta'] / $save['kurs'];
		$save['mavro'] = round($save['mavro'], 4);
		$save['ostatok'] = $save['mavro'];
		$id = 0;
		$this->MOper->save($save, $id);
		redirect("oper/?u[]=".$save['user_id']);
	}

	function sort_by_date($user_id = null)
	{
		$user_id = intval($user_id);
		if(!$this->MUser->inMyStructure($user_id))
		{
			return;
		}
		$opers = $this->MOper->getList(array('user_id' => $user_id, 'sort' => 'date asc,id asc'));
		$num = 1;
		foreach($opers as $oper)
		{
			$save = array('num' => $num);
			$this->MOper->save($save, $oper['id']);
			$num++;
		}
		redirect("oper/?u[]=".$user_id);
	}

	function recount($id = 0)
	{
		return;
		$id = intval($id);
		$oper = $this->MOper->get($id);
		if(@$oper['id'])
		{
			$this->load->library('Operation');
			$this->operation->updateAllBonusByUser($oper['user_id']);
		}
		redirect("oper/#u".$oper['user_id']);
	}

	function recount_des($oper_id = 0)
	{
		global $USER_LEVEL;
		$oper_id = intval($oper_id);
		$oper = $this->MOper->get($oper_id);
		if(!$oper['id'] || !$this->MUser->inMyStructure($oper['user_id']) || !canRecountDes($oper))
		{
			return;
		}
		$user = $this->MUser->get($oper['user_id']);
		$this->load->library('Operation');
		$filters = array('parent_id' => $oper['id'], 'sort' => 'id asc');
		foreach($USER_LEVEL as $k => $v)
		{
			$filters['type'][] = 'l'.$k;
		}
		$all_bonus_des = $this->MOper->getList($filters);
		$all_bonus_ref = $this->MOper->getList(array('parent_id' => $oper['id'], 'type' => 'ref'));
		$all = array_merge($all_bonus_des, $all_bonus_ref);
		foreach($all_bonus_des as $o)
		{
			$all = array_merge($all, $this->operation->getAllChildren($o));
		}
		foreach($all_bonus_ref as $o)
		{
			$all = array_merge($all, $this->operation->getAllChildren($o));
		}
		foreach($all as $o)
		{
			$this->MOper->delete($o['id']);
		}
		if($oper['_tmp_add_des'])
		{
			$this->operation->addAllBonusRukovod($oper, $oper['_tmp_des_summ']);
		}
		if($oper['_tmp_add_ref'])
		{
			if($user['referer_email'])
			{
				$ref_user = $this->MUser->getFirstRow(array('email' => $user['referer_email']));
				if(@$ref_user['id'])
				{
					$this->operation->addRefBonus($oper, $ref_user, $user, $oper['_tmp_des_summ']);
				}
			}
		}
		redirect("oper/?u[]=".$oper['user_id']);
	}

	function recount_frozen($oper_id)
	{
		$oper = $this->MOper->get($oper_id);
		operUpdateFrozen($oper);
		redirect("oper/?u[]=".$oper['user_id']);
	}

	function frozen()
	{
		global $TYPE_OPER, $USER_LEVEL_BONUS_TXT;
		$filters = array();
		$filters['frozen_date>='] = date('Y-m-d');
		$filters['ostatok>'] = 0;
		$filters['depo_only'] = true;
		$filters['sort'] = 'frozen_date asc,id asc';
		$filters['user_parent_id'] = Auth::$user_id;
		$filters['add_fields'] = array('user_fio', 'user_email');
		$filters['type'] = array('buy' => 'buy', 'ng' => 'ng', 'patriot' => 'patriot', 'arest' => 'arest', '8marta' => '8marta', 'modify.buy' => 'modify.buy', 'week' => 'week');

		$filter = array(
			'show_on' => '1month',
			'type_inc' => array('ref', 'nodepo')
		);

		if(@$_GET['show_on'])
		{
			$filter['show_on'] = $_GET['show_on'];
			$filter['type_inc'] = @$_GET['type_inc'] && is_array($_GET['type_inc']) ? $_GET['type_inc'] : array();
		}

		if($filter['show_on'] == 'week') $filters['frozen_date<='] = date('Y-m-d', strtotime("+1 week"));
		if($filter['show_on'] == '1month') $filters['frozen_date<='] = date('Y-m-d', strtotime("+1 month"));
		if($filter['show_on'] == '2month') $filters['frozen_date<='] = date('Y-m-d', strtotime("+2 months"));
		if($filter['show_on'] == '3month') $filters['frozen_date<='] = date('Y-m-d', strtotime("+3 months"));

		if(in_array('nodepo', $filter['type_inc'])) unset($filters['depo_only']);
		if(in_array('ref', $filter['type_inc'])) $filters['type']['ref'] = 'ref';
		if(in_array('reg', $filter['type_inc'])) $filters['type']['reg'] = 'reg';
		if(in_array('l', $filter['type_inc']))
		{
			foreach($USER_LEVEL_BONUS_TXT as $k => $v)
			{
				$filters['type']['l'.$k] = 'l'.$k;
			}
		}
		if(in_array('my', $filter['type_inc']))
		{
			unset($filters['user_parent_id']);
			$children = $this->MUser->getList(array('parent_id' => Auth::$user_id));
			$users_id = array_keys($children);
			$users_id[] = Auth::$user_id;
			$filters['user_id'] = $users_id;
		}

		$opers = $this->MOper->getList($filters);

		$this->load->view('head', array());
		$this->load->view('oper/frozen', array('operations' => $opers, 'filter' => $filter));
		$this->load->view('foot');
	}

	function info($oper_id = null)
	{
		$opers = array();
		$oper_id = intval($oper_id);
		$oper = $this->MOper->get($oper_id);
		if(!@$oper['id'])
		{
			return;
		}
		$me = $this->MUser->get(Auth::$user_id);
		$user = $this->MUser->getFirstRow(array('id' => $oper['user_id'], 'hi_like' => $me['hi']));
		if(!@$user['id'])
		{
			return;
		}

		$platezh_id = array();
		$parent_id = $oper['parent_id'];
		$i = 0;
		while($parent_id && $i < 50)
		{
			$parent = $this->MOper->get($parent_id);
			if(@$parent['id'])
			{
				$parent['user'] = $parent['user_id'] == $user['id'] ? $user : $this->MUser->get($parent['user_id']);
				$opers[] = $parent;
				if($parent['platezh_id'])
				{
					$platezh_id[] = $parent['platezh_id'];
				}
			}
			$parent_id = @$parent['parent_id'];
			$i++;
		}
		$opers = array_reverse($opers);

		$oper['user'] = $user;
		$oper['selected_view'] = true;
		$opers[] = $oper;
		if($oper['platezh_id'])
		{
			$platezh_id[] = $oper['platezh_id'];
		}

		$children = $this->operation->getAllChildren($oper);
		$children_index = array();
		foreach($children as $ch)
		{
			$ch['user'] = $ch['user_id'] == $user['id'] ? $user : $this->MUser->get($ch['user_id']);
			$children_index[$ch['id']] = $ch;
			if($ch['user_id'] == Auth::$user_id || $ch['user_id'] == $oper['user_id'] && $ch['platezh_id'])
			{
				$platezh_id[] = $ch['platezh_id'];
			}
		}
		ksort($children_index);
		foreach($children_index as $ch)
		{
			$opers[] = $ch;
		}

		$platezh_filters = array('add_fields' => array('schet_name'));
		$platezh_filters['t.id'] = count($platezh_id) ? $platezh_id : -1;
		$platezhi = $this->MPlatezh->getList($platezh_filters);
		foreach($platezhi as $k => $p)
		{
			$koeff = isPlatezhBuy($p) ? -1 : 1;
			$p['itogo'] = $p['summ'] + $koeff * $p['komiss'];
			platezhFromTo($p);
			$platezhi[$k] = $p;
		}
		
		$this->load->view('head', array('small' => true));
		$this->load->view('oper/info', array('opers' => $opers, 'platezhi' => $platezhi));
		$this->load->view('foot');
	}

	function platezh_relation($oper_id = null)
	{
		$oper_id = intval($oper_id);
		$oper = $this->MOper->get($oper_id);
		if(!@$oper['id'])
		{
			return;
		}
		$me = $this->MUser->get(Auth::$user_id);
		$user = $this->MUser->getFirstRow(array('id' => $oper['user_id'], 'hi_like' => $me['hi']));
		if(!@$user['id'])
		{
			return;
		}
		$user['operations'] = array($oper['id'] => $oper);



		$platezhi_filters = array('from_date' => $oper['date'], 'add_fields' => array('schet_name'));
		if(isBuy($oper))
		{
			$platezhi_filters['to_user_id'] = Auth::$user_id;
			$platezhi_filters['type'] = array('buy', 'buy.drugoy', 'buy.des', 'buy.other');
		}
		else
		{
			$platezhi_filters['from_user_id'] = Auth::$user_id;
			$platezhi_filters['type'] = array('sell', 'sell.drugoy', 'sell.des', 'sell.other');
		}
		$platezhi = $this->MPlatezh->getList($platezhi_filters);


		if(@$_POST['go'])
		{
			if(@$_POST['platezh_id'] && isset($platezhi[$_POST['platezh_id']]))
			{
				$save = array('platezh_id' => intval($_POST['platezh_id']));
				$this->MOper->save($save, $oper['id']);
				$this->load->view('oper/platezh_relation_go', array('platezh' => $platezhi[$_POST['platezh_id']], 'oper' => $oper));
				return;
			}
		}

		foreach($platezhi as $k => $p)
		{
			$koeff = isPlatezhBuy($p) ? -1 : 1;
			$p['itogo'] = $p['summ'] + $koeff * $p['komiss'];
			platezhFromTo($p);
			$platezhi[$k] = $p;
		}

		$this->load->view('head', array('small' => true));
		$this->load->view('oper/platezh_relation', array('user' => $user, 'platezhi' => $platezhi));
		$this->load->view('foot');
	}

	
}

